package skean.apclient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.security.auth.PrivateCredentialPermission;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class BSInfoService extends Service {
	ServerSocket serverSocket;
	Socket socket;
	TelephonyManager tm;
	StringBuilder sb;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		try {
			serverSocket = new ServerSocket(12345);
			new Thread() {
				@Override
				public void run() {
					while (true) {
						try {
							socket = serverSocket.accept();
							new Thread(new WaitTask(socket)).start();
						}
						catch (Exception e) {
							e.printStackTrace();
						}
					}
				}

			}.start();
		}
		catch (IOException e) {
			Log.i("客户端", "建立出错");
			e.printStackTrace();
		}
	}

	private StringBuilder addBSInfo(StringBuilder sb) {
		String operator = tm.getNetworkOperator();
		sb.append("运营商:");
		sb.append(operator);
		sb.append("--基站信息: ");
		CellLocation location = tm.getCellLocation();
		if (location == null) {
			sb.append("出错");
		}
		else {
			sb.append(location.toString());
			if (location instanceof CdmaCellLocation) {
				sb.append("--基站类型:CDMA");
			}
			else if (location instanceof GsmCellLocation) {
				sb.append("--基站类型:GSM");
			}
		}
		return sb;
	}

	private class WaitTask implements Runnable {
		Socket socket;

		public WaitTask(Socket socket) {
			super();
			this.socket = socket;
		}

		@Override
		public void run() {
			String line = null;
			BufferedReader reader;
			PrintWriter writer;
			try {
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer = new PrintWriter(socket.getOutputStream());
				// StringBuilder sb = new StringBuilder();
				// sb = addBSInfo(sb);
				// writer.write(sb.toString());
				// writer.flush();
				while (true) {
					line = reader.readLine();
					if (line != null) {
						if (line.equals("get")) {
							StringBuilder sb = new StringBuilder();
							sb = addBSInfo(sb);
							writer.println(sb.toString());
							writer.flush();
							break;
						}
						else if (line.startsWith("reboot")) {
							String type = line.substring(6, 8);
							Process process = Runtime.getRuntime().exec("su");
							DataOutputStream os = new DataOutputStream(process.getOutputStream());
							os.writeBytes("sqlite3 /data/data/com.android.providers.settings/databases/settings.db\n");
							if (type.equals("2g")) {
								os.writeBytes("update  global set value=1 where name='preferred_network_mode';\n");
							}
							else if (type.equals("3g")) {
								os.writeBytes("update  global set value=2 where name='preferred_network_mode';\n");
							}
							else if (type.equals("4g")) {
								os.writeBytes("update  global set value=11 where name='preferred_network_mode';\n");
							}
							os.writeBytes(".exit\n");
							Runtime.getRuntime().exec("su -c \"/system/bin/reboot\"\n");
							break;
						}
						else
							continue;
					}
				}
				reader.close();
				writer.close();
				socket.close();
			}
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
